package org.dromara.crawler.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseController;
import org.dromara.crawler.domain.KnowledgeTemplate;
import org.dromara.crawler.service.KnowledgeTemplateService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识模版管理接口
 *
 * @author shup
 */
@Slf4j
@RequiredArgsConstructor
//@RestController
@RequestMapping("/knowledgeTemplate")
public class KnowledgeTemplateController extends BaseController {

    private final KnowledgeTemplateService templateService;

    /**
     * 查询模版
     */
    @GetMapping("/queryList")
    public R<List<KnowledgeTemplate>> queryList(KnowledgeTemplate knowledgeTemplate) {
        try {
            return R.ok(templateService.queryList(knowledgeTemplate));
        } catch (Exception e) {
            log.error("查询任务失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }

    }

    /**
     * 创建模版
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody KnowledgeTemplate knowledgeTemplate) {
        try {
            templateService.save(knowledgeTemplate);
            return R.ok("创建成功");
        } catch (Exception e) {
            log.error("创建失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }

    }

    /**
     * 删除模版
     */
    @DeleteMapping("/deleteById")
    public R<String> deleteById(@RequestParam Long id) {
        try {
            templateService.removeById(id);
            return R.ok("删除成功");
        } catch (Exception e) {
            log.error("删除失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }

    }

    /**
     * 分页查询
     */
    @GetMapping("/queryByPage")
    public R<IPage<KnowledgeTemplate>> queryByPage(KnowledgeTemplate knowledgeTemplate, PageQuery pageQuery) {
        try {
            return R.ok(templateService.queryByPage(knowledgeTemplate, pageQuery));
        } catch (Exception e) {
            log.error("查询数据失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/getById")
    public R<KnowledgeTemplate> getById(Long id) {
        try {
            return R.ok(templateService.getById(id));
        } catch (Exception e) {
            log.error("查询数据失败，原因：" + e.getMessage());
            return R.fail(e.getMessage());
        }
    }

}
